import { createRouter, createWebHistory } from 'vue-router'
import Cookies from 'js-cookie'
import { userInforStore } from '@/stores/user'
const init_route = [
  {
    path: '/index',
    name: 'Index',
    component: () => import('@/views/index.vue'),
    meta: {
      title: '首页',
      is_show: true
    }
  },
  {
    path: '/task',
    name: 'task',
    component: () => import('@/views/home/task.vue'),
    meta: {
      title: '任务管理',
      is_show: true,
      keepAlive : true
    },
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('@/views/home/search.vue'),
    meta: {
      title: '搜索发布',
      is_show: true,
      keepAlive : true
    }
  },
  {
    path: '/information/manage',
    name: 'manage',
    component: () => import('@/views/home/Information/manage.vue'),
    meta: {
      title: '信息管理',
      is_show: true,
      keepAlive : true
    }
  },
  {
    path: '/information',
    name: 'Information',
    // route level code-splitting
    // this generates a separate chunk (About.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('@/views/home/Information/index.vue'),
    meta: {
      title: '信息管理',
      path: '/information',
      is_show: true,
      keepAlive : true
    },
    children: [
      // {
      //   path: 'manage',
      //   name: 'manage',
      //   // route level code-splitting
      //   // this generates a separate chunk (About.[hash].js) for this route
      //   // which is lazy-loaded when the route is visited.
      //   component: () => import('@/views/home/Information/manage.vue'),
      //   meta: {
      //     title: '信息管理',
      //     is_show: true,
      //     keepAlive : true
      //   }
      // },
      {
        path: 'payment',
        name: 'payment',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/Information/payment.vue'),
        meta: {
          title: '付款设置',
          is_show: true
        }
      }
    ]
  },
  {
    path: '/maintenance',
    name: 'maintenance',
    // route level code-splitting
    // this generates a separate chunk (About.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('@/views/home/maintenance/index.vue'),
    meta: {
      title: '维护管理',
      path: '/maintenance',
      is_show: true
    },
    children: [
      {
        path: 'form',
        name: 'form',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/maintenance/form.vue'),
        meta: {
          title: '表单维护',
          is_show: true
        }
      },
      {
        path: 'category',
        name: 'category',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/maintenance/category.vue'),
        meta: {
          title: '标签维护',
          is_show: true
        }
      }
    ]
  },
  {
    path: '/users',
    name: 'users',
    // route level code-splitting
    // this generates a separate chunk (About.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('@/views/home/users/index.vue'),
    meta: {
      title: '用户管理',
      path: '/user',
      is_show: true
    },
    children: [
      {
        path: 'list',
        name: 'list',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/users/list.vue'),
        meta: {
          title: '用户列表',
          is_show: true
        }
      },
      {
        path: 'application',
        name: 'application',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/users/application.vue'),
        meta: {
          title: '维护申请',
          is_show: true
        }
      },
      {
        path: ':id',
        name: 'detail',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/users/detail.vue'),
        meta: {
          title: '用户详情',
          is_show: false
        }
      }
    ]
  },
  {
    path: '/data',
    name: 'data',
    // route level code-splitting
    // this generates a separate chunk (About.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('@/views/home/data/index.vue'),
    meta: {
      title: '数据统计',
      path: '/data',
      is_show: true
    },
    children: [
      {
        path: 'newusers',
        name: 'newusers',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/data/newusers.vue'),
        meta: {
          title: '新用户统计',
          is_show: true
        }
      },
      {
        path: 'user',
        name: 'user',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/data/user.vue'),
        meta: {
          title: '用户统计',
          is_show: true
        }
      },
      {
        path: 'orders',
        name: 'orders',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/data/orders.vue'),
        meta: {
          title: '订单统计',
          is_show: true
        }
      },
      {
        path: 'finance',
        name: 'finance',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/data/finance.vue'),
        meta: {
          title: '财务统计',
          is_show: true
        }
      },
      {
        path: 'outstanding',
        name: 'outstanding',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/data/outstanding.vue'),
        meta: {
          title: '维护人业绩统计',
          is_show: true
        }
      },
      {
        path: 'publisher',
        name: 'publisher',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/data/publisher.vue'),
        meta: {
          title: '发布人统计',
          is_show: true
        }
      }
    ]
  },
  {
    path: '/permissions',
    name: 'permissions',
    // route level code-splitting
    // this generates a separate chunk (About.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('@/views/home/permissions/index.vue'),
    meta: {
      title: '权限管理',
      path: '/permissions',
      is_show: true
    },
    children: [
      {
        path: 'employee',
        name: 'employee',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/permissions/employee.vue'),
        meta: {
          title: '员工管理',
          is_show: true
        }
      },
      {
        path: 'department',
        name: 'department',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/permissions/department.vue'),
        meta: {
          title: '部门管理',
          is_show: true
        }
      }
    ]
  },
  {
    path: '/system',
    name: 'system',
    // route level code-splitting
    // this generates a separate chunk (About.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('@/views/home/system/index.vue'),
    meta: {
      title: '系统配置',
      is_show: true
    },
    children: [
      {
        path: 'contribute',
        name: 'contribute',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/system/contribute.vue'),
        meta: {
          title: '贡献海报',
          is_show: true
        }
      },
      {
        path: 'drainage',
        name: 'drainage',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/system/drainage.vue'),
        meta: {
          title: '引流海报',
          is_show: true
        }
      },
      {
        path: 'sensitive',
        name: 'sensitive',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('@/views/home/system/sensitive.vue'),
        meta: {
          title: '敏感词管理',
          is_show: true
        }
      }
    ]
  }
]
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      redirect: '/index',
      meta: {
        is_show: true
      },
      children: [
        {
          path: '/index',
          name: 'Index',
          component: () => import('@/views/index.vue'),
          meta: {
            title: '首页',
            is_show: true
          }
        },
        {
          path: '/login',
          name: 'login',
          // route level code-splitting
          // this generates a separate chunk (About.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('@/views/login.vue'),
          meta: {
            title: '登录',
            is_show: false
          }
        },
        {
          path: '/forgot',
          name: 'forgot',
          // route level code-splitting
          // this generates a separate chunk (About.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('@/views/forgot.vue'),
          meta: {
            title: '忘记密码',
            is_show: false
          }
        },
        {
          path: '/detail',
          name: 'taskdetail',
          component: () => import('@/views/home/detail.vue'),
          meta: {
            title: '任务详情',
            is_show: false
          }
        },
        ...init_route
      ]
    },
    {
    path: '/detail',
    name: 'taskdetail',
    component: () => import('@/views/home/detail.vue'),
    meta: {
      title: '任务详情',
      is_show: false
    }
  },
    {
      path: '/404',
      name: '404',
      component: () => import('@/views/404.vue'),
      meta: {
        is_show: false
      }
    },
    {
      path: '/:pathMatch(.*)',
      redirect: '/404',
      meta: {
        title: '敏感词管理',
        is_show: false
      }
    }
  ]
})
const whiteList = ['/login']

router.beforeEach((to, from, next) => {
  // 判断有没有登录
  if (Cookies.get('access_token')) {
    if (to.path == '/login') return next('/')
    let path_list: any = []
    const store = userInforStore()
    if(store.menu.length<=0){
      Cookies.remove('access_token')
      return next('/login')
    }
    store.menu.forEach((item: any) => {
      if(item.children.length<=0){
        path_list.push(item.path.trim())
      }
      if (item.children.length > 0) {
        item.children.forEach((item_twe: any) => {
          path_list.push(item_twe.path.trim())
        })
      }
    })
    // console.log(to.name,"to.nameto.nameto.name",path_list,router);
    
    if(path_list.includes(to.path)||to.name =="detail" ||to.name =="taskdetail"){
      next()
    }else{
      if(to.path=="/404"){
        next()
      }else{
        next("/404")
      }
    }
   
  } else {
    whiteList.includes(to.path) ? next() : next('/login')
  }
})
export default router
